// pages/ShopOwnerPages/myService/myService.js
var util = require('../../../utils/util')
Page({
  data: {
    isLoading: false,
    tabArr: [{
        tabName: '待审核',
        state: 1,
        listArr: [],
        page: 1,
      },
      {
        tabName: '待上架',
        state: 3,
        page: 1,
        listArr: []
      },
      {
        tabName: '已上架',
        state: 4,
        page: 1,
        listArr: []
      },
      {
        tabName: '未通过',
        state: 2,
        page: 1,
        listArr: []
      },
    ],
    tab_page: 0
  },

  onChange(e) {
    console.log(e)
    //请求每列表第一页内容
    let index = e.detail.index;
    this.setData({
      tab_page: index
    })
    if (this.data.tabArr[index].listArr.length === 0) {
      this.setData({
        isLoading: true
      })
      let data = {
        page: this.data.tabArr[index].page,
        limit: 10,
        state: this.data.tabArr[index].state,
        keywords: ''
      }
      this.getGood(data, index)
    }
  },

  addGood() {
    wx.navigateTo({
      url: '/pages/ShopOwnerPages/addGood/addGood',
    })
  },

  getGood(data, index = 0) {
    util.sendRequest('shop/goods/list', 'GET', data, 1).then(res => {
      if (res.data.code === 0) {
        let result = res.data.result
        console.log(result)
        this.setData({
          [`tabArr[${index}].listArr`]: result.data,
          [`tabArr[${index}].loadFinish`]: true,
        })
      }
    })
  },

  getHeight(el, name) {
    //获取元素高度
    let query = wx.createSelectorQuery();
    query.select(el).boundingClientRect(rect => {
      let height = rect.height;
      // console.log(height);
      this.setData({
        [name]: height
      })
    }).exec();
  },


  onLoad: function (options) {
    this.getHeight('.search_hover', 'searchHeight');
    this.setData({
      navHeight: getApp().globalData.navHeight,
      hoverHeightNotab: getApp().globalData.hoverHeightNotab,
      tabStick: this.data.searchHeight + getApp().globalData.navHeight,
      device: getApp().globalData.device,
    })
    //获取第一项的列表
    let data = {
      page: 1,
      limit: 10,
      state: 1,
      keywords: ''
    }
    this.getGood(data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})